package cn.zzuli.activemq.persistent;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import javax.xml.soap.Text;
import java.io.IOException;

public class JmsConsumer1 {
    private static final String ACTIVEMQ_URL = "tcp://81.70.205.143:61617";

    private static final String TOPIC_NAME = "topic01";

    public static void main(String[] args) throws JMSException, IOException {
        System.out.println(" *****  1 ");
        //1.创建链接工厂
        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        //2.通过连接工厂获得获得链接connection,并且设置订阅的id，根据id区分每个订阅者
        Connection connection = mqConnectionFactory.createConnection();
        connection.setClientID("设置id");


        //3.创建会话session
        //3.1第一个参数 事务，第二个参数 签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4.创建目的地，此处使用主题 topic
        Topic topic = session.createTopic(TOPIC_NAME);

        /**
         * 创建主题订阅者，订阅
         * 参数一：订阅哪个主题   参数二：备注
         */
        TopicSubscriber subscriber = session.createDurableSubscriber(topic, "remark");

        /**
         * 在此处开始启动链接
         */
        connection.start();
        //5.主题订阅者开始接收消息

        Message message = subscriber.receive();
        while (message!=null){
            TextMessage textMessage = (TextMessage) message;
            System.out.println(textMessage);
            message=subscriber.receive(1000L);
        }
        session.close();
        connection.close();
    }
}
